﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Label.h"
#include "Scale9Image.h"
#include "Text.h"
#include "../base/EventResize.h"
#include "../base/EventMouse.h"
#include "../render/Camera.h"
#include "Font.h"

namespace SkyEngine2d
{
	
	Label* Label::create(Widget* parent /*= nullptr*/)
	{
		auto p = makeWidgetPtr(parent);
		if (p && p->init())
		{
			if (p->getParent()) p->getParent()->addChildren(p);
			return p;
		}
		return nullptr;
	}
	
	Label::~Label()
	{
	
	}
	
	void Label::setText(const std::wstring& text)
	{
		m_text = text;
		if (m_textRender)
		{
			m_textRender->setText(text);
		}
		else
		{
			m_textRender = std::make_shared<Text>(m_text);
			auto p = getStyleInfor();
			if (p)
			{
				m_textRender->setFont(p->textFont);
				m_textRender->setColor(p->textColor);
			}
			m_textRender->getFont().setAlignment(m_aligment);
			m_textRender->setDrawRect(Rect(getAbsPos(), getSize()));
			m_textRender->getFont().setWordWrapping(m_isWordWrap);
		}
	}
	
	const std::wstring& Label::getText() const
	{
		return m_text;
	}
	
	const std::unique_ptr<Scale9Image>& Label::getImage() const
	{
		return m_image;
	}
	
	void Label::setImage(std::unique_ptr<Scale9Image> image)
	{
		m_image = std::move(image);
		m_image->setDisplaySize(getSize());
	}
	
	bool Label::wordWrap() const
	{
		return m_isWordWrap;
	}
	
	void Label::setWordWrap(bool word_wrap)
	{
		m_isWordWrap = word_wrap;
		if (m_textRender)
		{
			m_textRender->getFont().setWordWrapping(word_wrap);
		}
	}
	
	Alignment Label::getAlignemt() const
	{
		return m_aligment;
	}
	
	void Label::setAlignment(Alignment alignment)
	{
		m_aligment = alignment;
		if (m_textRender)
		{
			m_textRender->getFont().setAlignment(alignment);
		}
	}
	
	Label::Label(Widget*parent /*= nullptr*/)
		:Widget(parent)
		, m_aligment(Alignment::AlignCenter)
		, m_isWordWrap(true)
	{
		this->setFocusPolicy(FocusPolicy::NoFocus);
	}
	
	int Label::getDefultWidth() const
	{
		return 100;
	}
	
	int Label::getDefultHeight() const
	{
		return 50;
	}
	
	void Label::draw(IRender* render, const Matrix4X4& transform)
	{
		if (m_image)
		{
			DirectX::XMMATRIX mvp = DirectX::XMMatrixMultiply(transform, getCamera()->getViewProjectMat());
			m_image->draw(render, mvp);
		}
	
		if (m_textRender)
		{
			m_textRender->draw(render, Matrix4X4::IdentityMat);
		}
	}
	
	void Label::onMoveEvent(EventWidgetMove* e)
	{
		Widget::onMoveEvent(e);
		if (m_textRender)
		{
			m_textRender->setDrawRect(Rect(getAbsPos(), getSize()));
		}
	}
	
	void Label::onResizeEvent(EventResize* e)
	{
		Widget::onResizeEvent(e);

		if (m_textRender)
		{
			m_textRender->setDrawRect(Rect(getAbsPos(), e->getNewSize()));
		}
	
		if (m_image)
		{
			m_image->setDisplaySize(e->getNewSize());
		}
	}
	
	void Label::onWidgetStateChange()
	{
		this->updateStyleInfo();
	}
	
	void Label::onWidgetStyleChange()
	{
		this->updateStyleInfo();
	}
	
	void Label::updateStyleInfo()
	{
		auto p = this->getStyleInfor();
		if (p)
		{
			if (m_textRender)
			{
				m_textRender->setFont(p->textFont);
				m_textRender->setColor(p->textColor);
			}
		}
	}
}

